package com.link.base.base.superset.controller;

import com.alibaba.fastjson.JSONObject;
import com.link.base.base.superset.model.Superset;
import com.link.base.base.superset.service.AuthorizeService;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * superset的认证功能
 * @author Li Yang
 */
@Controller
@RequestMapping("/superset")
public class AuthorizeController {

    @Resource
    private AuthorizeService authorizeService;

    /**
     * 验证access_token
     *
     * @param accessToken
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/checktoken", produces = "application/json")
    @ResponseBody
    public Superset checkTokenRequestParam(@RequestParam("access_token") String accessToken,
                                             HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(accessToken)) {
            return null;
        }
        Superset result = null;
        try {
            String jsonStr = authorizeService.checkAccessTokenReturnObject(accessToken);
            System.out.println("#superset# : " + jsonStr);
            result = JSONObject.parseObject(jsonStr, Superset.class);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return result;
    }

    /**
     * 验证access_token
     *
     * @param accessToken
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/checktoken/{accessToken}", produces = "application/json")
    @ResponseBody
    public Superset checkTokenPathParam(@PathVariable String accessToken, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(accessToken)) {
            return null;
        }
        Superset result = null;
        try {
            String jsonStr = authorizeService.checkAccessTokenReturnObject(accessToken);
            System.out.println("#superset# : " + jsonStr);
            result = JSONObject.parseObject(jsonStr, Superset.class);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return result;
    }

}
